package com.freak.wanandroid.model.system;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.freak.wanandroid.R;
import com.freak.wanandroid.base.fragment.BaseLifecycleFragment;
import com.freak.wanandroid.bean.SystemClassChildrenEntity;
import com.freak.wanandroid.bean.SystemClassifyBean;
import com.freak.wanandroid.bean.SystemDetailBean;
import com.freak.wanandroid.model.home.activity.ArticleDetailAct;
import com.freak.wanandroid.model.login.LoginActivity;
import com.freak.wanandroid.model.system.adapter.SystemClassifyAdapter;
import com.freak.wanandroid.model.system.adapter.SystemDetailAdapter;
import com.freak.wanandroid.net.CommonLiveData;
import com.freak.wanandroid.util.ToastUtil;
import com.freak.wanandroid.util.ToolUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 体系
 *
 * @author: freak
 * @date: 2022/9/21
 */
public class SystemFragment extends BaseLifecycleFragment<SystemViewModel> {

    private RecyclerView mSystemFirstRecycle;

    private SystemClassifyAdapter mSystemFirstAdapter;

    private List<SystemClassifyBean> mClassifyBeanList;

    private List<SystemClassChildrenEntity> mChildrenList;

    private int mCurPage = 1;

    private List<SystemDetailBean.Datas> mDatas;

    private SystemDetailAdapter mSystemDetailAdapter;

    private static int mId;

//    /**
//     * 断网重连
//     *
//     * @param networkType
//     */
//    @Override
//    public void onNetConnected(NetworkType networkType) {
//        super.onNetConnected(networkType);
//        mViewModel.getSystem();
//    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_system;
    }

    @Override
    protected void initEventAndData(View view) {
        mDatas = new ArrayList<>();
        mClassifyBeanList = new ArrayList<>();
        mSystemFirstRecycle = view.findViewById(R.id.system_first_recycle);
        initRecycle();
    }

    private void initRecycle() {
        mSystemFirstRecycle.setLayoutManager(new LinearLayoutManager(mCurrentActivity));
        mSystemFirstAdapter = new SystemClassifyAdapter(R.layout.item_system_classify, mClassifyBeanList, mCurrentActivity);
        mSystemFirstAdapter.bindToRecyclerView(mSystemFirstRecycle);
        mSystemFirstRecycle.setAdapter(mSystemFirstAdapter);
//        mSystemDetailRecycle.setLoadingListener(new XRecyclerView.LoadingListener() {
//            @Override
//            public void onRefresh() {
//                mCurPage = 1;
//                mViewModel.getClassifyDetail(String.valueOf(mCurPage), String.valueOf(mId));
//                showLoading();
//            }
//
//            @Override
//            public void onLoadMore() {
//                mCurPage++;
//                mViewModel.getClassifyDetail(String.valueOf(mCurPage), String.valueOf(mId));
//                showLoading();
//            }
//        });
    }

    @Override
    protected void onResumeLoadData() {
//        mViewModel.getSystem();
//        showLoading();
    }

    @Override
    public void onResume() {
        super.onResume();
        mViewModel.getSystem();
        showLoading();
    }

    /**
     * 获取一级与二级分类成功回调
     *
     * @param systemClassifyBeanList
     */
    public void getSystemSuccess(List<SystemClassifyBean> systemClassifyBeanList) {
        dismissLoading();
        if (mCurPage == 1) {
            mClassifyBeanList.clear();
        }
        mClassifyBeanList.addAll(systemClassifyBeanList);
        mSystemFirstAdapter.notifyDataSetChanged();
    }

    /**
     * 获取分类列表成功回调
     *
     * @param systemDetailBean
     */
    public void getClassifyDetailSuccess(SystemDetailBean systemDetailBean) {
        dismissLoading();
        if (systemDetailBean != null) {
            List<SystemDetailBean.Datas> dataList = systemDetailBean.getDatas();
            if (mCurPage == 1) {
                refresh(dataList);
            } else {
                loading(dataList);
            }
        }
    }

    private void loading(List<SystemDetailBean.Datas> mList) {
        if (mList != null && mList.size() != 0) {
            mDatas.addAll(mList);
            mSystemDetailAdapter.setData(mDatas);
            mSystemDetailAdapter.notifyDataSetChanged();
        } else {
        }
    }

    private void refresh(List<SystemDetailBean.Datas> mList) {
        if (mList != null && mList.size() != 0) {
            mDatas.clear();
            mDatas.addAll(mList);
            if (mSystemDetailAdapter == null) {
                mSystemDetailAdapter = new SystemDetailAdapter(getActivity(), mDatas);
                mSystemDetailAdapter.setOnItemClickListener(new SystemDetailAdapter.OnItemClickListener() {
                    @Override
                    public void doCollage(SystemDetailBean.Datas mData, ImageView imageView) {
                        if (mData.getCollect()) {
                            mViewModel.doCancelCollect(mData.getId());
                        } else {
                            mViewModel.doCollect(mData.getId());
                        }
                        showLoading();
                    }

                    @Override
                    public void doIntern(SystemDetailBean.Datas mData) {
                        Intent intent = new Intent(getActivity(), ArticleDetailAct.class);
                        intent.putExtra("url", mData.getLink());
                        intent.putExtra("title", mData.getTitle());
                        startActivity(intent);
                    }
                });
            } else {
                mSystemDetailAdapter.setData(mDatas);
                mSystemDetailAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void dataObserver() {
        mViewModel.mDoCollect.observe(this, this::doCollectSuccessOrError);
        mViewModel.mDoCancelCollect.observe(this, this::doCancelCollectSuccessOrError);
        mViewModel.mGetSystem.observe(this, this::getSystemSuccess);
        mViewModel.mGetSystemError.observe(this, s -> {
            dismissLoading();
        });
//        mViewModel.mGetClassifyDetail.observe(this, this::getClassifyDetailSuccess);
        mViewModel.mGetClassifyDetailError.observe(this, s -> {
            dismissLoading();
        });
    }

    /**
     * 收藏成功或失败处理
     *
     * @param commonLiveData CommonLiveData
     */
    public void doCollectSuccessOrError(CommonLiveData commonLiveData) {
        dismissLoading();
        if (CommonLiveData.SUCCESS == commonLiveData.code) {
            mCurPage = 1;
            mViewModel.getClassifyDetail(String.valueOf(mCurPage), String.valueOf(mId));
            ToastUtil.showShort(getActivity(), "收藏成功");
            showLoading();
            return;
        }
        ToastUtil.showShort(getActivity(), "登录过期，请重新登录");
        ToolUtils.logout(getActivity());
        LoginActivity.startAction(getActivity());
    }

    public void doCancelCollectSuccessOrError(CommonLiveData commonLiveData) {
        dismissLoading();
        if (CommonLiveData.SUCCESS == commonLiveData.code) {
            mCurPage = 1;
            mViewModel.getClassifyDetail(String.valueOf(mCurPage), String.valueOf(mId));
            ToastUtil.showShort(getActivity(), "取消收藏成功");
            showLoading();
            return;
        }
        ToastUtil.showShort(getActivity(), "登录过期，请重新登录");
        ToolUtils.logout(getActivity());
        LoginActivity.startAction(getActivity());
    }
}
